/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

int guifilechooserdialog_ModalOpenDialog( struct ZlangRuntime *rt , struct ZlangObject *guifilechooserdialog_obj , struct ZlangObject *guiwindow_obj , char *title , int action , char *filefilter , char *filename , struct ZlangObject *out )
{
	struct ZlangDirectProperty_guifilechooserdialog	*guifilechooserdialog_direct_prop = GetObjectDirectProperty(guifilechooserdialog_obj) ;
	struct ZlangDirectProperty_guiwindow		*guiwindow_direct_prop = GetObjectDirectProperty(guiwindow_obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guiboxlayout_common = (struct ZlangDirectProperty_guiwidget_common *)guiwindow_direct_prop ;

	if( action == GUIFILECHOOSERACTION_OPEN )
	{
		guifilechooserdialog_direct_prop->gtk_dialog = gtk_file_chooser_native_new( title , GTK_WINDOW(guiboxlayout_common->gtk_widget) , action , "_Open" , "_Cancel" ) ;
	}
	else if( action == GUIFILECHOOSERACTION_SAVE )
	{
		guifilechooserdialog_direct_prop->gtk_dialog = gtk_file_chooser_native_new( title , GTK_WINDOW(guiboxlayout_common->gtk_widget) , action , "_Save" , "_Cancel" ) ;
	}
	else if( action == GUIFILECHOOSERACTION_SELECT_FOLDER )
	{
		guifilechooserdialog_direct_prop->gtk_dialog = gtk_file_chooser_native_new( title , GTK_WINDOW(guiboxlayout_common->gtk_widget) , action , "_Open" , "_Cancel" ) ;
	}
	if( filefilter )
	{
		GtkFileFilter	*filter = NULL ;
		char		*s = NULL ;
		char		*s_offsetptr = NULL ;
		char		*t = NULL ;
		char		*t_offsetptr = NULL ;
		unsigned char	default_flag ;

		/* "Text Files:*.txt,*.md|!All Files:*" */
		s = STRTOK_R( filefilter , "|" , & s_offsetptr ) ;
		while( s )
		{
			filter = gtk_file_filter_new() ;
			default_flag = FALSE ;
		
			t = STRTOK_R( s , ":" , & t_offsetptr ) ;
			if( (*t) == '!' )
			{
				default_flag = TRUE ;
				t++;
			}
			gtk_file_filter_set_name( filter , t );
			t = STRTOK_R( NULL , "," , & t_offsetptr ) ;
			while( t )
			{
				gtk_file_filter_add_pattern( filter , t );

				t = STRTOK_R( NULL , "," , & t_offsetptr ) ;
			}

			gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(guifilechooserdialog_direct_prop->gtk_dialog) , filter );
			if( default_flag == TRUE )
				gtk_file_chooser_set_filter( GTK_FILE_CHOOSER(guifilechooserdialog_direct_prop->gtk_dialog) , filter );

			s = STRTOK_R( NULL , "|" , & s_offsetptr ) ;
		}
	}
	if( filename )
	{
		gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER(guifilechooserdialog_direct_prop->gtk_dialog) , filename );
	}

	guifilechooserdialog_direct_prop->response = GtkNativeDialogRun( rt , GTK_NATIVE_DIALOG(guifilechooserdialog_direct_prop->gtk_dialog) ) ;
	if( guifilechooserdialog_direct_prop->response == GTK_RESPONSE_ACCEPT )
		guifilechooserdialog_direct_prop->response = GTK_RESPONSE_OK ;
	if( guifilechooserdialog_direct_prop->response == GTK_RESPONSE_OK )
	{
		GFile		*file = NULL ;
		char		*uri = NULL ;
		char		*filename = NULL ;
		char		*p = NULL ;

		memset( guifilechooserdialog_direct_prop->pathfilename , 0x00 , sizeof(guifilechooserdialog_direct_prop->pathfilename) );
		file = gtk_file_chooser_get_file( GTK_FILE_CHOOSER(guifilechooserdialog_direct_prop->gtk_dialog) ) ;
		uri = g_file_get_uri(file);
		filename = g_filename_from_uri( uri , NULL , NULL ) ;
		strncpy( guifilechooserdialog_direct_prop->pathfilename , filename , sizeof(guifilechooserdialog_direct_prop->pathfilename)-1 );
		p = GetFilenamePtr( guifilechooserdialog_direct_prop->pathfilename ) ;
		if( p )
		{
			strcpy( guifilechooserdialog_direct_prop->filename , p );
		}
		else
		{
			strcpy( guifilechooserdialog_direct_prop->filename , guifilechooserdialog_direct_prop->pathfilename );
		}

		g_object_unref( file );
		g_free( uri );
		g_free( filename );
	}
	g_object_unref( guifilechooserdialog_direct_prop->gtk_dialog );
	CallRuntimeFunction_int_SetIntValue( rt , out , guifilechooserdialog_direct_prop->response );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int;
int ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*title = NULL ;
	int32_t						action ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in2 , & title , NULL );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & action );

	return guifilechooserdialog_ModalOpenDialog( rt , obj , in1 , title , action , NULL , NULL , out1 );
}

ZlangInvokeFunction ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string;
int ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*title = NULL ;
	int32_t						action ;
	char						*filefilter = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in2 , & title , NULL );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & action );
	CallRuntimeFunction_string_GetStringValue( rt , in4 , & filefilter , NULL );

	return guifilechooserdialog_ModalOpenDialog( rt , obj , in1 , title , action , filefilter , NULL , out1 );
}

ZlangInvokeFunction ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string_string;
int ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*in4 = GetInputParameterInLocalObjectStack(rt,4) ;
	struct ZlangObject				*in5 = GetInputParameterInLocalObjectStack(rt,5) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*title = NULL ;
	int32_t						action ;
	char						*filefilter = NULL ;
	char						*filename = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in2 , & title , NULL );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & action );
	CallRuntimeFunction_string_GetStringValue( rt , in4 , & filefilter , NULL );
	CallRuntimeFunction_string_GetStringValue( rt , in5 , & filename , NULL );

	return guifilechooserdialog_ModalOpenDialog( rt , obj , in1 , title , action , filefilter , filename , out1 );
}

ZlangInvokeFunction ZlangInvokeFunction_guifilechooserdialog_GetPathFilename;
int ZlangInvokeFunction_guifilechooserdialog_GetPathFilename( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guifilechooserdialog	*guifilechooserdialog_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , guifilechooserdialog_direct_prop->pathfilename , STRLEN_OF_STRING );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guifilechooserdialog_GetFilename;
int ZlangInvokeFunction_guifilechooserdialog_GetFilename( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guifilechooserdialog	*guifilechooserdialog_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , guifilechooserdialog_direct_prop->filename , STRLEN_OF_STRING );
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guifilechooserdialog;
void *ZlangCreateDirectProperty_guifilechooserdialog( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guifilechooserdialog	*guifilechooserdialog_prop = NULL ;
	
	guifilechooserdialog_prop = (struct ZlangDirectProperty_guifilechooserdialog *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guifilechooserdialog) ) ;
	if( guifilechooserdialog_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guifilechooserdialog_prop , 0x00 , sizeof(struct ZlangDirectProperty_guifilechooserdialog) );
	
	return guifilechooserdialog_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guifilechooserdialog;
void ZlangDestroyDirectProperty_guifilechooserdialog( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guifilechooserdialog	*guifilechooserdialog_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( guifilechooserdialog_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guifilechooserdialog;
void ZlangSummarizeDirectPropertySize_guifilechooserdialog( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guifilechooserdialog) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guifilechooserdialog =
	{
		ZLANG_OBJECT_guifilechooserdialog , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guifilechooserdialog , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guifilechooserdialog , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guifilechooserdialog , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guifilechooserdialog;
struct ZlangObject *ZlangImportObject_guifilechooserdialog( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangObject	*prop = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guifilechooserdialog , & direct_funcs_guifilechooserdialog , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	prop = AddPropertyInObject( rt , obj , GetIntObjectInRuntimeObjectsHeap(rt) , "ACTION_OPEN") ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_int_SetIntValue( rt , prop , GUIFILECHOOSERACTION_OPEN );
	SetConstantObject( prop );
	
	prop = AddPropertyInObject( rt , obj , GetIntObjectInRuntimeObjectsHeap(rt) , "ACTION_SAVE") ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_int_SetIntValue( rt , prop , GUIFILECHOOSERACTION_SAVE );
	SetConstantObject( prop );
	
	prop = AddPropertyInObject( rt , obj , GetIntObjectInRuntimeObjectsHeap(rt) , "ACTION_SELECT_FOLDER") ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_int_SetIntValue( rt , prop , GUIFILECHOOSERACTION_SELECT_FOLDER );
	SetConstantObject( prop );
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int , ZLANG_OBJECT_int , "ModalOpenDialog" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string , ZLANG_OBJECT_int , "ModalOpenDialog" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guifilechooserdialog_ModalOpenDialog_guiwindow_string_int_string_string , ZLANG_OBJECT_int , "ModalOpenDialog" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_string,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guifilechooserdialog_GetPathFilename , ZLANG_OBJECT_string , "GetPathFilename" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guifilechooserdialog_GetFilename , ZLANG_OBJECT_string , "GetFilename" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

